var gentlepy="http://localhost:8011";
var totalFoldingPay = $("#totalFoldingPay").val();  //折后金额
var totlDeductionAmount = $("#totlDeductionAmount").val();  //优惠券抵扣
var totaldepositAmount = $("#totaldepositAmount").val();  //返现抵扣
document.getElementById("ys1").innerText = parseFloat(totalFoldingPay - totlDeductionAmount - totaldepositAmount).toFixed(1);

$("#addWay").click(function () {//添加收费方式
    var serviceActualPay = $("#totleServiceActualPay").val();  //应收金额
    var mode = $(this).parent().children().eq(0).clone();
    var money = 0;
    for (var i = 0; i < $(".amountOfBalance2").length; i++) {
        money += parseFloat($(".amountOfBalance2").eq(i).val())
    }
    if (serviceActualPay < money) {
        alert("此次缴费金额为" + serviceActualPay + ",请重新输入！");
        return;
    } else if (serviceActualPay == money) {
        return;
    }
    var s = parseFloat(serviceActualPay - money).toFixed(2);
    mode.find('.amountOfBalance2').val(s);
    $(this).parent().append(mode)
});

$("#reduceWay").click(function () {//删除收费方式
    var serviceActualPay = $("#totleServiceActualPay").val();  //应收金额
    var num = $(this).parent().children('.whiteBox').length;
    if (num > 1) {
        $(this).parent().children('.whiteBox').eq(num - 1).remove();
        var money = 0;
        for (var i = 0; i < ($(".amountOfBalance2").length - 1); i++) {
            money += parseFloat($(".amountOfBalance2").eq(i).val())
        }
        var s = parseFloat(serviceActualPay - money).toFixed(2);
        $(".amountOfBalance2").eq($(".amountOfBalance2").length - 1).val(s);
    }
});

amountOfBalance = function (obj) {
    obj.value = obj.value.replace(/[^\d.]/g, "");  //清除“数字”和“.”以外的字符
    obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的
    obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
    obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');//只能输入两个小数
    if (obj.value.indexOf(".") < 0 && obj.value != "") {//以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
        obj.value = parseFloat(obj.value);
    }
};

var addBalanceCount = 0;
rechargeCheckOut = function () {
    addBalanceCount++;
    if (addBalanceCount == 1) {
        var customerId = $("#customerId").val();
        var reservationsId = $("#reservationsId").val();
        var getTime = $("#getTime").val();
        var operationTime = $("#operationTime").val();
        var reservationsIdx = $("#reservationsIdx").val();
        var orthodontics = $("#orthodontics").val(); //正畸编号
        var totleServiceActualPay = $("#totleServiceActualPay").val();  //实收金额
        var payorrefund = document.getElementById('payorrefund').value;   //退款还是结账
        var id = document.getElementById("id").value;
        var str = [];
        var endtr = "";
        var amountOfBalance = 0;
        if (totleServiceActualPay == "" || totleServiceActualPay == null || totleServiceActualPay == "null") {
            totleServiceActualPay = 0;
        }
        var reg = new RegExp(",","g");
        var remarks = ($("#remarks").val()).replace(reg,'，');
        for (var i = 0; i < $(".checkoutMethodName").length; i++) {
            if ($(".amountOfBalance2").eq(i).val() == "" || $(".amountOfBalance2").eq(i).val() == null) {
                alert("请输入结账金额！");
                addBalanceCount = 0;
                return;
            } else if (payorrefund == '结账' || payorrefund == '还欠款') {
                endtr =  $(".checkoutMethodName").eq(i).val() + "#" + $(".amountOfBalance2").eq(i).val()+"#"+remarks ;
                amountOfBalance += parseFloat($(".amountOfBalance2").eq(i).val());
                str.push(endtr)
            } else if (payorrefund == '退款') {
                endtr = $(".checkoutMethodName").eq(i).val() + "#-" + $(".amountOfBalance2").eq(i).val()+"#"+remarks;
                amountOfBalance += parseFloat($(".amountOfBalance2").eq(i).val());
                str.push(endtr)
            } else if (payorrefund == '消费') {
                endtr =  $(".checkoutMethodName").eq(i).val() + "#" + $(".amountOfBalance2").eq(i).val()+"#"+remarks;
                amountOfBalance += parseFloat($(".amountOfBalance2").eq(i).val());
                str.push(endtr)
            }
        }
        if (parseFloat(amountOfBalance).toFixed(2) > parseFloat(totleServiceActualPay).toFixed(2)) {
            alert("结账金额不应大于折后金额！");
            addBalanceCount = 0;
            return;
        }
        if (parseFloat(amountOfBalance).toFixed(2) < parseFloat(totleServiceActualPay).toFixed(2)) {
            alert("结账未结束！请继续,应收金额为：" + totleServiceActualPay);
            addBalanceCount = 0;
            return;
        }
        addBalanceCount++;
        if (addBalanceCount == 2) {
            $.ajax({
                url: Feng.ctxPath + "/recharge/checkOut",
                type: "post",
                traditional: true,
                data: {
                    "reservationsIdx": reservationsIdx,
                    "customerId": customerId,
                    "reservationsId": reservationsId,
                    "operationTime": operationTime,
                    "getTime": getTime,
                    "totleServiceActualPay1": totleServiceActualPay,
                    "str": str,
                    "payorrefund": payorrefund,
                    "orthodontics": orthodontics
                },
                success: function (data) {
                    if (data == "1") {
                        Feng.success("结账成功！");
                        if (parent.$("#isDorj").val() == 'jz') {
                            var index = parent.layer.open({
                                type: 2,
                                title: '开单打印',
                                area: ['60%', '100%'], //宽高
                                fix: true, //不固定
                                maxmin: true,
                                content: Feng.ctxPath + '/consultant/to_print/' + id,
                                success: function () {
                                    window.parent.Recharge.table.refresh();
                                    window.parent.Recharge.total();
                                    parent.layer.close(parent.layer.getFrameIndex(window.name));
                                }
                            });
                            this.layerIndex = index;
                        } else {
                            var index = parent.layer.open({
                                type: 2,
                                title: '开单打印',
                                area: ['60%', '100%'], //宽高
                                fix: true, //不固定
                                maxmin: true,
                                content: Feng.ctxPath + '/consultant/to_print/' + id,
                                success: function () {
                                    parent.layer.close(parent.layer.getFrameIndex(window.name));
                                }
                            });
                            this.layerIndex = index;
                        }
                    } else {
                        alert(data);
                        if (parent.$("#isDorj").val() == 'jz') {
                            window.parent.Recharge.table.refresh();
                            window.parent.Recharge.total();
                            parent.layer.close(parent.layer.getFrameIndex(window.name));
                        } else {
                            window.parent.location.reload();
                            parent.layer.close(parent.layer.getFrameIndex(window.name));
                        }
                    }
                },
                error: function (data) {
                    alert('没有权限，请联系管理员!');
                }
            });
        }
    }
};

$(function () {
    $.ajax({
        url: Feng.ctxPath + "/consultant/getOpenBillListjz",
        type: "post",
        data: {
            "operationTime": $("#operationTime").val(),
            "getTime": $("#getTime").val(),
            "reservationsId": $("#reservationsId").val()
        },
        success: function (data) {
            var project = ""; //项目信息
            var entryName = "<div style='display: flex;line-height: 26px;padding-bottom: 10px;'><div style='font-size: 15px;font-weight: bolder;'>项目名称</div><div style='padding-left: 20px;'>";//项目名称
            for (var i = 0; i < data.length; i++) {
                project += ' <div  style="display: flex;padding: 2px;text-align: center ">' +
                    '<div style="flex: 2;" class="beyond" title="' + data[i].payServiceName + '">' + data[i].payServiceName + '</div>' +
                    '<div style="display: flex;flex-direction: column;flex: 1;height: 25px;">' +
                    '<div style="flex: 1;display: flex;border-bottom: 1px solid #999999">' +
                    '<div style="flex: 1;border-right: 1px solid #999999;line-height: 12px;text-align:right">' + data[i].leftUp + '</div>' +
                    '<div style="flex: 1;line-height: 12px;text-align:left">' + data[i].rightUp + '</div></div>' +
                    '<div style="flex: 1;display: flex"><div style="flex: 1;border-right: 1px solid #999999;line-height: 12px;text-align:right">' + data[i].leftDown + '</div>' +
                    '<div style="flex: 1;line-height: 12px;text-align:left">' + data[i].rightDown + '</div></div></div>' +
                    '<div style="flex: 1;">' + data[i].doctor + '</div>' +
                    '<div style="flex: 0.5;">' + data[i].unit + '</div>' +
                    '<div style="flex: 1;">' + data[i].payService.toFixed(2) + '元</div>' +
                    '<div style="flex: 0.5;">' + data[i].payServiceNum + '</div>' +
                    '<div style="flex: 1;">' + ((data[i].discountHide.toFixed(2)) * data[i].payServiceNum).toFixed(2) + '</div>' +
                    '<div style="flex: 1;">' + data[i].foldingPay.toFixed(2) + '元</div>' +
                    '<div style="flex: 1;">' + data[i].orderDiscount + '</div>' +
                    '<div style="flex: 1;">' + data[i].deductionAmount.toFixed(2) + '元</div>' +
                    '<div style="flex: 1;" >' + data[i].deductionTypeName + '</div>' +
                    '<div style="flex: 1;" >' + data[i].depositAmount.toFixed(2) + '元</div>' +
                    '<div style="flex: 1;">' + (parseFloat(data[i].foldingPay) - parseFloat(data[i].deductionAmount) - parseFloat(data[i].depositAmount)).toFixed(2) + '元</div>' +
                    '</div>';
                entryName += '<div>' + data[i].payServiceName + '</div>';
            }
            entryName += '</div></div>';
            if ($("#isDoNameValue").val() == "退款") {
                document.getElementById("payorrefund").value = "退款";
                $("#totalSum").html(entryName);
                if (parseInt(data[data.length - 1].totleServiceActualPay) == 0) {
                    $("#checkoutType").html("<span>本次退款<span style='color: red'>0.00</span></span>" +
                        "<span>已收金额<span>" + (parseFloat($("#totleServiceActualPay2").val()) + parseFloat($("#totaldepositAmount2").val())).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,') + "</span></span>");
                } else {
                    $("#checkoutType").html("<span>本次退款<span style='color: red'>" +
                        (parseFloat(data[data.length - 1].totleServiceActualPay) + parseFloat(data[data.length - 1].totleDepositAmount)).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,').substring(1) + "</span></span>" +
                        "<span>已收金额<span>" + (parseFloat($("#totleServiceActualPay2").val()) + parseFloat($("#totaldepositAmount2").val())).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,') + "</span></span>");
                }
                $("#doType").html("<span>本次退款</span>");
                $("#totleServiceActualPay").val(String(parseFloat(data[data.length - 1].totleServiceActualPay) + parseFloat(data[data.length - 1].totleDepositAmount)).substring(1));
                $(".amountOfBalance2").val(Math.abs(String(parseFloat(data[data.length - 1].totleServiceActualPay) + parseFloat(data[data.length - 1].totleDepositAmount)).substring(1)));
            } else if ($("#isDoNameValue").val() == "还欠款") {
                document.getElementById("payorrefund").value = "还欠款";
                $("#totalSum").html(entryName);
                if (parseInt(data[data.length - 1].totleServiceActualPay) == 0) {
                    $("#checkoutType").html("<span>本次补费<span  style='color: red'>0.00</span></span>" +
                        "<span>优惠抵扣<span >" + parseFloat(data[data.length - 1].totleDeductionAmount).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,') + "</span></span>" +
                        "<span>储值抵扣<span >" + parseFloat(data[data.length - 1].totleDepositAmount).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,') + "</span></span>" +
                        "<span>上次欠费<span>" + parseFloat($("#totleArrears").val()).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,') + "</span></span>")
                } else {
                    $("#checkoutType").html("<span>本次补费<span  style='color: red'>" + parseFloat(data[data.length - 1].totleServiceActualPay).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,') + "</span></span>" +
                        "<span>优惠抵扣<span >" + parseFloat(data[data.length - 1].totleDeductionAmount).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,') + "</span></span>" +
                        "<span>储值抵扣<span >" + parseFloat(data[data.length - 1].totleDepositAmount).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,') + "</span></span>" +
                        "<span>上次欠费<span>" + parseFloat($("#totleArrears").val()).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,') + "</span></span>")
                }
                $("#doType").html("<span>本次补费</span>");
                $("#totleServiceActualPay").val(data[data.length - 1].totleServiceActualPay);
                $(".amountOfBalance2").val(Math.abs(data[data.length - 1].totleServiceActualPay));
            } else if ($("#isDoNameValue").val() == "消费") {
                document.getElementById("payorrefund").value = "消费";
                $("#openBillList").html(project)
                $("#checkoutType").html("<span>本次消费<span>" + parseFloat(totalFoldingPay - totlDeductionAmount - totaldepositAmount).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,') + "</span></span>" +
                    "<span>本次缴费<span>" + parseFloat(data[data.length - 1].totleServiceActualPay).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,') + "</span></span>" +
                    "<span>欠费<span style='color: red'>" + parseFloat(data[data.length - 1].accountArrears).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,') + "</span></span>")
                $("#doType").html("<span>本次消费</span>");
                $("#totleServiceActualPay").val(data[data.length - 1].totleServiceActualPay);
                $(".amountOfBalance2").val(Math.abs(data[data.length - 1].totleServiceActualPay));
            }
        },
        error: function (data) {
            alert('没有权限，请联系管理员!');
        }
    });
});